﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using ScrumTable.BL.DM.DataManagement.Logging;
using ScrumTable.BL.DM.Properties;
using ScrumTable.Common.Monitoring;
using ScrumTable.DL.Data.Schema;

#endregion

namespace ScrumTable.BL.DM.DataManagement
{
    /// <summary>
    /// Contains the basic information about a project.
    /// </summary>
    public class Project : DomainBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private DomainCollection<Member> _members;
        private DomainCollection<Team> _teams;
        private DomainCollection<Iteration> _iterations;
        private DomainCollection<UserStory> _stories;
        private DomainCollection<Bug> _bugs;
        private ScrumItemTypeCollection _scrumItemTypes;
        private DomainCollection<Report> _reports;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets a list of all members.
        /// </summary>
        public DomainCollection<Team> Teams
        {
            get
            {
                if (_teams == null)
                {
                    _teams = new DomainCollection<Team>(
                        true,
                        Context,
                        this,
                        ContextInternal.Data.Storage[Schema.TeamSchema]);
                }
                return _teams;
            }
        }

        /// <summary>
        /// Gets a list of all members.
        /// </summary>
        public DomainCollection<Member> Members
        {
            get
            {
                if (_members == null)
                {
                    _members = new DomainCollection<Member>(
                        true,
                        Context,
                        this,
                        ContextInternal.Data.Storage[Schema.MemberSchema]);
                }
                return _members;
            }
        }

        /// <summary>
        /// Gets a list of all iterations.
        /// </summary>
        public DomainCollection<Iteration> Iterations
        {
            get
            {
                if (_iterations == null)
                {
                    _iterations = new DomainCollection<Iteration>(
                        true,
                        Context,
                        this,
                        ContextInternal.Data.Storage[Schema.IterationSchema]);
                }
                return _iterations;
           }
        }

        /// <summary>
        /// Gets a list of all stories associated to the product backlog.
        /// </summary>
        public DomainCollection<UserStory> Stories
        {
            get
            {
                if (_stories == null)
                {
                    _stories = new DomainCollection<UserStory>(
                        false,
                        Context,
                        this,
                        ContextInternal.Data.Storage[Schema.UserStorySchema]);
                }
                return _stories;
            }
        }

        /// <summary>
        /// Gets a list of all bugs.
        /// </summary>
        public DomainCollection<Bug> Bugs
        {
            get
            {
                if (_bugs == null)
                {
                    _bugs = new DomainCollection<Bug>(
                        false,
                        Context,
                        this,
                        ContextInternal.Data.Storage[Schema.BugSchema]);
                }
                return _bugs;
            }
        }

        /// <summary>
        /// Gets a list of all types.
        /// </summary>
        public ScrumItemTypeCollection Types
        {
            get
            {
                if (_scrumItemTypes == null)
                {
                    _scrumItemTypes = new ScrumItemTypeCollection(
                        Context,
                        this,
                        ContextInternal.Data.Storage[Schema.TypeSchema]);
                }
                return _scrumItemTypes;
            }
        }


        /// <summary>
        /// All definded reports 
        /// </summary>
        public DomainCollection<Report> Reports
        {
            get
            {
                if (_reports == null)
                {
                    _reports = new DomainCollection<Report>(
                        false,
                        Context,
                        this,
                        ContextInternal.Data.Storage[Schema.ReportSchema]);
                }
                return _reports;
            }
        }

        /// <summary>
        /// Gets the name of the current domain object. In most cases this
        /// property will contain the domain object title.
        /// </summary>
        public override string Name
        {
            get { return DataObject.GetData(Schema.ProjectSchema.Name, string.Empty); }
            set { }
        }

        /// <summary>
        /// Gets the parent collection instance.
        /// </summary>
        internal override IDomainCollection ParentCollection
        {
            get { return Context.Projects; }
        }

        /// <summary>
        /// Gets a collection which contains all child collections of the current
        /// domain object.
        /// </summary>
        internal override ICollection ChildCollections
        {
            get { return new IDomainCollection[] { Members, Iterations, Stories, Bugs, Reports }; }
        }

        /// <summary>
        /// Gets the domain object schema of the current domain object.
        /// </summary>
        protected override SchemaBase DomainSchema { get { return Schema.ProjectSchema; } }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------            

        /// <summary>
        /// Loads all data in the project. The call will be executed in an
        /// asynchronous manner.
        /// </summary>
        /// <param name="dataImportMonitor">Specifies the data import monitor in data layer level.</param>
        public void LoadData(IDataImportMonitor dataImportMonitor)
        {
            if (dataImportMonitor == null)
                dataImportMonitor = new NullDataImportMonitor();

            ActionLogger.InfoFormat(Resources.LogActProjLoadInitialData, Name, IdChain);
            EnsureNotDisposed();
            ContextInternal.Data.ImportData(IdChain, dataImportMonitor);
        }

        /// <summary>
        /// Saves all data in the project. The call will be executed in an
        /// asynchronous manner.
        /// </summary>
        /// <param name="callbackEvent">Specifies the callback which is fired after the
        /// process has been completed. The callback is of type ICancellableDataResult
        /// and its Data property contains true, if all changes have been accepted.</param>
        public void SaveData(AsyncCallback callbackEvent)
        {
            ContextInternal.Data.ExportData(callbackEvent);
        }

        /// <summary>
        /// Resets all child collections (IDomainCollection instances) of the
        /// current domain object.
        /// </summary>
        protected override void ResetChildCollections()
        {
            _members = null;
            _iterations = null;
            _stories = null;
            _bugs = null;
            _reports = null;
        }

        /// <summary>
        /// Initializes the mappings stored in the current instance.
        /// </summary>
        protected override void InitializeMappings()
        {
            base.InitializeMappings();

            PropertySchemaMappings.Add(Schema.ProjectSchema.Name, "Name");
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}